package com.wang.util;

/**
 * @author wcy
 * @date 2023-07-07 16:52
 */
public class CustomOneUtil {

    public static byte[] HexString2Bytes(String hexStrings) {

        String hexString = hexStrings.replaceAll(" ", "");

        if (hexString == null || hexString.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length() / 2; i++) {
            String subStr = hexString.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;

    }


    public static String bytes2HexString(byte[] b) {
        String r = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            r += hex.toUpperCase() + " ";
        }
        return r;
    }

    // 16进制字符串转换10进制
    public static int hex2decimal(String hex) {
        return Integer.parseInt(hex, 16);
    }

    // 16进制字符串转换10进制
    public static Float hex2decimalFloat(String hex) {
        Long l = Long.parseLong(hex, 16);
        Float f = Float.intBitsToFloat(l.intValue());
        return f;
    }

}
